package com.newbingo.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.StringUtils;

import com.newbingo.dao.imple.IGameTableDao;
import com.newbingo.entity.Game;
import com.newbingo.entity.GameOperationTime;
import com.newbingo.entity.GameTable;
import com.newbingo.hib.HibernateSessionFactory;
import com.newbingo.util.Pager;

public class GameTableDao extends HibernateDaoSupport implements IGameTableDao {
	private static final Log log = LogFactory.getLog(GameTableDao.class);

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#save(com.newbingo.entity.GameTable)
	 */
	public void save(GameTable transientInstance) {
		log.debug("saving GameTable instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#delete(com.newbingo.entity.GameTable)
	 */
	public void delete(GameTable transientInstance) {
		log.debug("deleting GameTable instance");
		try {
			GameTable gt=findByGameTableId(transientInstance.getGtno());
			getSession().delete(gt);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#merge(com.newbingo.entity.GameTable)
	 */
	public GameTable merge(GameTable transientInstance) {
		log.debug("merging GameTable instance");
		try {
			GameTable result = (GameTable) getSession()
					.merge(transientInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}

	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findAll()
	 */
	public List findAll() {
		// TODO Auto-generated method stub
		log.debug("finding all GameTable instances");
		try {
			String queryString = "from GameTable";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findByGameTableId(java.lang.Integer)
	 */
	public GameTable findByGameTableId(Integer id) {
		// TODO Auto-generated method stub
		log.debug("getting GameTable instance with id: " + id);
		try {
			GameTable instance = (GameTable) getSession().get(
					"com.newbingo.entity.GameTable", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findByName(java.lang.Object)
	 */
	public List findByName(Object name) {
		// TODO Auto-generated method stub

		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findByPage(int, int, int)
	 */
	public Pager findByPage(int start, int limit, int gameno) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(GameTable.class);
		// 判断是否有查询类型条件
        cri.createAlias("game", "game");
        if (gameno>0) {
            cri.add(Expression.eq("game.gameno", gameno));
		}
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List list = cri.list();
		pager.setData(list);
		return pager;
	}
	
	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		log.debug("finding GameTable instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GameTable as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#findByTableState(java.lang.Object)
	 */
	public List findByTableState(Object tableState) {
		// TODO Auto-generated method stub
		return findByProperty(STATE, tableState);
	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#attachClean(com.newbingo.entity.GameTable)
	 */
	public void attachClean(GameTable instance) {
		log.debug("attaching clean GameTable instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}

	}

	/* (non-Javadoc)
	 * @see com.newbingo.dao.IMemberDao#attachDirty(com.newbingo.entity.GameTable)
	 */
	public void attachDirty(GameTable instance) {
		log.debug("attaching dirty GameTable instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}

	}
	


}
